<?php

class GalactusViewTelescope extends GalactusView {

	public $control = NULL;

	function __construct($control) {
		GalactusView::__construct($control);
	}

	public function retrieveCSS( ) {
		return '
			.carousel-indicators > li.active {
				background: #555753 !important;
			}

			.carousel-inner > .item > img {
				width: 100% !important;
			}

			.form-horizontal .btn:first-child {
				margin-left: 0px !important;
			}
		';
	}

	public function retrieveJS( ) {
		return '
			function galactus_view_telescope_show_add_edit( ) {
				$("#image-text").click(function( ) { $("#image-file").trigger("click"); });
				$("#image-file").change(function( ) { $("#image-text").val(document.getElementById("image-file").files[0].name); });
			}

			function galactus_view_telescope_show_default( ) {
				$(".carousel-inner > .item").each(function( ) {
					$(".carousel-indicators").append(\'<li data-target="#carousel" data-slide-to="\' + $(this).index( ) + \'" class="shadowinner"></li>\');
				});
				if($(".carousel-inner > .item").size( ) > 0) {
					$(".carousel-control:first").trigger("click");
				}
				$(".pagination a").click(function( ) {
					var cmd = "'.URL.'" + $(this).attr("galactus-command");
					$("#contentarea").load(cmd, { params: $(".carousel-inner > .item.active").attr("telescope-id") });
				});
			}
		';
	}

	public function retrieveImage($telescope_id) {
		$telescope = $this->control->model->retrieveTelescope($telescope_id);
		header('Content-type: image/'.$telescope[0]["image_type"]);
		header('Content-Disposition: inline; filename="img.'.$telescope[0]["image_type"].'"');
		echo $telescope[0]["image"];
	}

	public function show($command, $telescope_id = NULL) {
		switch($command[2]) {
			case "ADD": $this->showAddEdit( ); break;
			case "EDIT": $this->showAddEdit($telescope_id); break;
			default: $this->showDefault( ); break;
		}
	}

	private function showAddEdit($telescope_id = -1) {
		$action = URL.'TELESCOPE/EDIT';
		$telescope = $this->control->model->retrieveTelescope($telescope_id);
		if(empty($telescope)) {
			$action = URL.'TELESCOPE/ADD';
			$telescope = array("telescope_id" => NULL, "name" => NULL, "model_id" => NULL, "link_control" => NULL, "link_camera" => NULL, "brief" => NULL);
		}
		$options = "";
		$telescope_models = $this->control->model->retrieveTelescopeModels( );
		for($i = 0; $i < count($telescope_models); ++$i) {
			$selected = ($telescope[0]["model_id"] == $telescope_models[$i]["id"])? "selected" : NULL;
			$options .= '<option value="'.$telescope_models[$i]["id"].'" '.$selected.'>'.$telescope_models[$i]["model"].'</option>';
		}
		$content = '
			<form method="post" action="'.$action.'" enctype="multipart/form-data" class="form-horizontal offset2">
				<input type="hidden" name="params[ ]" class="span4" value="'.$telescope[0]["telescope_id"].'" />
				<div class="control-group">
					<label class="control-label" for="name">Nome</label>
					<div class="controls">
						<input type="text" name="params[ ]" class="span4" id="name" value="'.$telescope[0]["name"].'" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="image-text">Imagem</label>
					<div class="controls">
						<input type="text" class="span4" id="image-text" />
						<input type="file" name="params[ ]" accept="image/*" class="hide" id="image-file" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="model">Modelo</label>
					<div class="controls">
						<select name="params[ ]" class="span4" id="model">'.$options.'</select>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="link-control">Link de Controle</label>
					<div class="controls">
						<input type="url" name="params[ ]" class="span4" id="link-control" value="'.$telescope[0]["link_control"].'" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="link-camera">Link da Câmera</label>
					<div class="controls">
						<input type="url" name="params[ ]" class="span4" id="link-camera" value="'.$telescope[0]["link_camera"].'" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="brief">Descrição</label>
					<div class="controls">
						<textarea rows="6" name="params[ ]" class="span4" id="brief">'.$telescope[0]["brief"].'</textarea>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button type="reset" class="btn span2">Limpar</button>
						<button type="submit" class="btn btn-primary span2">Confirmar</button>
					</div>
				</div>
			</form>
			<script type="text/javascript"> $(galactus_view_telescope_show_add_edit( )); </script>
		';
		echo $content;
	}

	private function showDefault( ) {
		$carousel_items = "";
		$telescopes = $this->control->model->retrieveTelescope( );
		for($i = 0; $i < count($telescopes); ++$i) {
			$carousel_items .= '
				<div class="item" telescope-id="'.$telescopes[$i]['telescope_id'].'">
					<img src="'.URL.'TELESCOPE/RETRIEVE_IMAGE?params='.$telescopes[$i]['telescope_id'].'" />
					<div class="carousel-caption">
						<h4><a href="'.$telescopes[$i]['link_maker'].'">'.$telescopes[$i]['name'].': '.$telescopes[$i]['model'].'</a></h4>
						<p>'.$telescopes[$i]['brief'].'</p>
					</div>
				</div>
			';
		}
		$command = "TELESCOPE/STREAM_STOP";
		$icon = "icon-stop";
		$title = "Desativa";
		exec("pgrep ffserver", $pids);
		if(empty($pids)) {
			$command = "TELESCOPE/STREAM_START";
			$icon = "icon-play";
			$title = "Ativa";
		}
		$buttons = array(
			'<li><a galactus-command="TELESCOPE/SHOW/ADD" title="Adiciona um telescópio"><i class="icon-plus-sign"></i></a></li>',
			'<li><a galactus-command="TELESCOPE/SHOW/EDIT" title="Edita o telescópio selecionado"><i class="icon-edit"></i></a></li>',
			'<li><a galactus-command="TELESCOPE/REMOVE" title="Remove o telescópio selecionado"><i class="icon-minus-sign"></i></a></li>',
			'<li><a galactus-command="CALENDAR/SHOW" title="Gerencia o calendário do telescópio selecionado"><i class="icon-calendar"></i></a></li>',
			'<li><a galactus-command="SCHEDULE/SHOW" title="Agenda um horário para o telescópio selecionado"><i class="icon-th"></i></a></li>',
			'<li><a galactus-command="CONTROL/SHOW" title="Acessa o telescópio selecionado"><i class="icon-eye-open"></i></a></li>',
			'<li><a galactus-command="'.$command.'" title="'.$title.' streaming dos telescópios"><i class="'.$icon.'"></i></a></li>'
		);
		switch($this->control->profile_id) {
			case ADMINISTRATOR: $buttons = implode("", $buttons); break;
			case USER: $buttons = $buttons[4].$buttons[5]; break;
			default: $buttons = ""; break;
		}
		echo '
			<div class="container-fluid">
				<div class="row-fluid">
					<div class="carousel offset2 slide span8" id="carousel">
						<ol class="carousel-indicators"></ol>
						<div class="carousel-inner">'.$carousel_items.'</div>
						<a class="carousel-control left" href="#carousel" data-slide="prev">&lsaquo;</a>
						<a class="carousel-control right" href="#carousel" data-slide="next">&rsaquo;</a>
					</div>
				</div>
				<div class="row-fluid"><div class="pagination pagination-centered"><ul>'.$buttons.'</ul></div></div>
			</div>
			<script type="text/javascript"> $(galactus_view_telescope_show_default( )); </script>
		';
	}

}
?>
